package com.woniuxy.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.portal.service.BookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.portal.model.entity.Book;

import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/top")
    @ApiOperation("查询销量最高的前N本书籍")
    public ResponseEntity<Page<Book>> findTopSales(@RequestBody Map<String, Integer> params) {
        //获取页码和页面大小
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        //调用业务层查询
        Page<Book> page = bookService.findTopSales(pageNum, pageSize);
        return ResponseEntity.ok(page);
    }

    @PostMapping("/findByType")
    @ApiOperation("按类型显示书籍")
    public ResponseEntity<Page<Book>> findByType(@RequestBody Map<String, Integer> params) {
        //获取页码和页面大小
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        //获取分类类型
        int typeId = params.get("typeId");
        //调用业务层查询
        Page<Book> page = bookService.findByType(pageNum, pageSize, typeId);
        return ResponseEntity.ok(page);
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("通过ID查询书籍")
    public ResponseEntity<Book> findById(@PathVariable("id") Long id) {
        Book book = bookService.findById(id);
        return ResponseEntity.ok(book);
    }


}
